﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Platform.Http;
using Web.Class;

namespace Web.Pages.YongShui
{
    public partial class BuMenSZ : BasePage
    {
        #region 属性
        //gridView主键
        private int BuMenXXID
        {
            get
            {
                return Convert.ToInt32(ViewState["_BuMenXXID"]);
            }
            set
            {
                ViewState["_BuMenXXID"] = value;
            }
        }
        //保存结果集
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }
        #endregion

        #region 变量
        
         YS_JX_BUMENXX _YS_JX_BUMENXX= new YS_JX_BUMENXX();
         YS_JX_BUMENXXBLL _YS_JX_BUMENXXBLL = new YS_JX_BUMENXXBLL();
         SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        #endregion
        
        #region 事件
        //页面加载事件
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitControl();
                InitData();                
                Display();
                LoadData();
            }
            
        }

        protected override void Render(HtmlTextWriter sender)
        {
            base.Render(sender);
        }

        /// <summary>
        /// grid添加
        /// </summary>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            //初始化数据
            InitData();
            this.Status = GridViewStatus.Add;
            Display();
        }

        /// <summary>
        /// grid放弃
        /// </summary>
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.Status = GridViewStatus.Init;
            Display();
        }

        /// <summary>
        /// grid查看
        /// </summary>
        protected void lnbSearch_Command(object sender, CommandEventArgs e)
        {
            this.BuMenXXID = Convert.ToInt32(e.CommandArgument);
            this.Status = GridViewStatus.Search;
            Display();
            SetData();

        }
        
        /// <summary>
        /// grid修改
        /// </summary>
        protected void lnbModify_Command(object sender, CommandEventArgs e)
        {
            this.BuMenXXID = Convert.ToInt32(e.CommandArgument);
            this.Status = GridViewStatus.Update;
            Display();
            SetData();
        }

        /// <summary>
        /// grid删除
        /// </summary>
        protected void lnbDel_Command(object sender, CommandEventArgs e)
        {
            this.BuMenXXID = Convert.ToInt32(e.CommandArgument);
            //执行逻辑删除
            _YS_JX_BUMENXX = _YS_JX_BUMENXXBLL.SelectYS_JX_BUMENXX(this.BuMenXXID, null);
            
            this._YS_JX_BUMENXX.S_CAOZUOR = LoginInfo.CurrentUser.Account;
            this._YS_JX_BUMENXX.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
            this._YS_JX_BUMENXX.I_JLZT = -1;
            _SYS_LOGBLL.Log<YS_JX_BUMENXX>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "部门信息", OperateState.Delete, _YS_JX_BUMENXX, null);
            if (_YS_JX_BUMENXXBLL.InsertOrUpdate(_YS_JX_BUMENXX, null))
            {
                
                LoadData();
                this.Status = GridViewStatus.Init;
                Display();
                PageTools.AlertMsg(this,"操作成功!");
            }
        }

        /// <summary>
        /// grid保存
        /// </summary>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }
       
        #endregion

        #region 方法
        /// <summary>
        /// 初始化控件
        /// </summary>
        private void InitControl()
        {
            //加载站点
            PageTools.BindComboBox(this.cmbZhanDian, BusinessTool.GetTextAndValue("YS_JX_ZHANDIAN", "S_ZHANDIANMC", "S_ZHANDIANBH", null,null).Tables[0]);
            this.cmbZhanDian.SelectedIndex = 0;
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            this.txtBuMenBH.Text = "";
            this.txtBuMenMC.Text = "";
            this.txtBuMenMS.Text = "";
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        private void LoadData()
        {
            this.dtSource= BusinessTool.GetViewYS_JX_BUMENXX().Tables[0];
            this.gvList.DataSource = this.dtSource;
            this.gvList.DataBind();
        }

        /// <summary>
        ///用于控制添加、修改、查看三种状态的显示
        /// </summary>
        private void Display()
        {
            switch (this.Status)
            {
                case GridViewStatus.Init:
                    btnAdd.Visible = true;
                    GridViewContentTable.Visible = false;
                    this.gvList.Enabled = true;
                    SetText(false);
                    break;
                case GridViewStatus.Search:
                    btnAdd.Visible = true;
                    GridViewContentTable.Visible = true;
                    SetText(false);
                    break;
                case GridViewStatus.Update:
                    btnAdd.Visible = false;
                    GridViewContentTable.Visible = true;
                    this.gvList.Enabled = false;
                    this.lblOperateTip.Text = "修改";
                    SetText(true);
                    this.txtBuMenBH.Enabled = false;
                    this.txtBuMenMC.Focus();
                    break;
                case GridViewStatus.Add:
                    btnAdd.Visible = false;
                    GridViewContentTable.Visible = true;
                    this.gvList.Enabled = false;
                    this.lblOperateTip.Text = "添加";
                    SetText(true);
                    this.txtBuMenBH.Enabled = true;     
                    this.txtBuMenBH.Focus();                                   
                    break;
            }
        }

        /// <summary>
        /// 设置输入框状态
        /// </summary>
        /// <param name="b"></param>
        private void SetText(bool b)
        {
           
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        private void SaveData()
        {
            OperateState operateState;
            //添加
            if (this.Status == GridViewStatus.Add)
            {
                operateState = OperateState.Add;
                _YS_JX_BUMENXX.ID = 0;
                _YS_JX_BUMENXX.I_JLZT = 0;
            }
            //修改
            else
            {
                //修改操作
                operateState = OperateState.Modify;
                _YS_JX_BUMENXX = _YS_JX_BUMENXXBLL.SelectYS_JX_BUMENXX(this.BuMenXXID, null);
            }
            //判断部门编号是否存在
            if (_YS_JX_BUMENXXBLL.SelectYS_JX_BUMENXXesDynamic(string.Format("I_JLZT=0 and S_BIANHAO='{0}' and ID<>'{1}'", this.txtBuMenBH.Text, this._YS_JX_BUMENXX.ID), null).Rows.Count > 0)
            {
                PageTools.AlertMsg(this,"您所输入部门编号系统中已经存在使用,请修改。");
                //弹框
                return;
            }
            _YS_JX_BUMENXX.S_BIANHAO = this.txtBuMenBH.Text;
            _YS_JX_BUMENXX.S_MINGCHENG = this.txtBuMenMC.Text;
            _YS_JX_BUMENXX.S_MIAOSHU = this.txtBuMenMS.Text;
            _YS_JX_BUMENXX.S_ZHANDIANBH = this.cmbZhanDian.Value.ToString();
            _YS_JX_BUMENXX.S_CAOZUOR = LoginInfo.CurrentUser.Account;
            _YS_JX_BUMENXX.D_CAOZUOSJ = DateTime.Now;
            _SYS_LOGBLL.Log<YS_JX_BUMENXX>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "部门信息", operateState, _YS_JX_BUMENXX, null);
            if (_YS_JX_BUMENXXBLL.InsertOrUpdate(_YS_JX_BUMENXX, null))
            {
                
                PageTools.AlertMsg(this, "保存成功！");                
                this.Status = GridViewStatus.Init;
                Display();
                LoadData();
                //定位记录
                this.BuMenXXID = Web.Class.PageTools.FocusOpreateRecord(this.dtSource, this.gvList, "S_BIANHAO", _YS_JX_BUMENXX.S_BIANHAO, true);
    
            }
            else
            {
                PageTools.AlertMsg(this,"保存失败！");
            }
        }


        /// <summary>
        /// 给输入框赋值
        /// </summary>
        private void SetData()
        {
            _YS_JX_BUMENXX = _YS_JX_BUMENXXBLL.SelectYS_JX_BUMENXX(this.BuMenXXID, null);
            this.txtBuMenBH.Text = _YS_JX_BUMENXX.S_BIANHAO;
            this.txtBuMenMC.Text = _YS_JX_BUMENXX.S_MINGCHENG;
            this.txtBuMenMS.Value = _YS_JX_BUMENXX.S_MIAOSHU;
            this.cmbZhanDian.Value = _YS_JX_BUMENXX.S_ZHANDIANBH;
        }

       
        #endregion

        
    }
}